package com.fe.class03;


import java.util.Stack;

/**
 * @Description 有限长度的数组实现栈
 * @Author sosuke :-)
 * @Date 2021/12/11 06:05
 */
public class Code05_ArrayToStack {
    public static class MyStack {
        private int[] arr;
        private int index; // 入栈的位置
        private final int limit;

        // index=0表示空，index=limit表示满
        public MyStack(int limit) {
            this.limit = limit;
            arr = new int[limit];
            index = 0;
        }

        public void push(int data) {
            if (isFull()) {
                throw new RuntimeException("栈满了，不能再加了");
            }
            arr[index++] = data;
        }

        public int pop() {
            if (isEmpty()) {
                throw new RuntimeException("栈空了，不能再取了");
            }
            return arr[--index];
        }

        public boolean isEmpty() {
            return index <= 0;
        }

        public boolean isFull() {
            return index >= limit;
        }

        // public static void main(String[] args) {
        //     MyStack myStack = new MyStack(5);
        //     System.out.println(myStack.pop());
        //     myStack.push(1);
        //     myStack.push(2);
        //     myStack.push(3);
        //     myStack.push(4);
        //     myStack.push(5);
        //     System.out.println(myStack.pop());
        //     System.out.println(myStack.pop());
        //     System.out.println(myStack.pop());
        //     System.out.println(myStack.pop());
        //     System.out.println(myStack.pop());
        //     System.out.println(myStack.pop());
        // }

        public static void main(String[] args) {
            int testTimes = 10000;
            int maxValue = 100;
            int maxSize =  15;
            for (int i = 0; i < testTimes; i++) {
                int size = (int) (Math.random() * (maxSize + 1));
                MyStack myStack = new MyStack(size);
                Stack<Integer> stack = new Stack<>();
                for (int j = 0; j < size; j++) {
                    int num = (int) (Math.random() * maxValue);
                    if (stack.isEmpty()) {
                        if (!myStack.isEmpty()) {
                            System.out.println("MyStack oops (isEmpty)");
                        }
                        stack.push(num);
                        myStack.push(num);
                    } else {
                        if (Math.random() < 0.5) {
                            stack.push(num);
                            myStack.push(num);
                        } else {
                            if (!isEqual(stack.pop(), myStack.pop())) {
                                System.out.println("MyStack oops");
                            }
                        }
                    }
                }
            }
            System.out.println("Nice");
        }

        private static boolean isEqual(Integer o1, Integer o2) {
            if (null == o1 && null != o2) return false;
            if (null != o1 && null == o2) return false;
            if (null == o1) return true;
            return o1.equals(o2);
        }
    }

}
